package com.training.service;

import com.training.common.Analyst;
import com.training.common.model.Result;
import com.training.common.model.Subject;
import com.training.common.model.Train;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AnalystServiceImp implements AnalystService {


    @Override
    public Analyst getAnalyst(String trainids, String subjectid,String studentid) {
        Subject subject = Subject.dao.findById(subjectid);
        Analyst analyst = new Analyst();
        analyst.setSubjectname(subject.getName());
        String[] trainidarray = trainids.split(",");
        Train train = null;
        List<String> xAxisData = new ArrayList<String>();
        List<Double> seriesData = new ArrayList<Double>();
        List<Result> resultList = null;
        DecimalFormat df = new DecimalFormat(".00");
        boolean intervalboo = true;
        for (String trainid : trainidarray){
            train = Train.dao.findById(trainid);
            xAxisData.add(train.getName());
            resultList = Result.dao.getTrainSubectidsResult(trainid,subjectid,studentid);
            double resultfloat = 0.00;
            for(Result result : resultList){
                resultfloat = resultfloat + Double.parseDouble(result.getResult().replace('′','.'));
            }
            if(resultfloat % 1 == 0){
            }else{
                intervalboo = false;
            }
            seriesData.add(resultfloat);
        }
        analyst.setMin((int)Math.round(Collections.min(seriesData))-1);
        analyst.setMax((int)Math.round(Collections.max(seriesData))+1);
        if(intervalboo){
            analyst.setInterval(1);
        }else{
            analyst.setInterval(0.5);
        }
        analyst.setxAxisData(xAxisData);
        analyst.setSeriesData(seriesData);
        return analyst;
    }
}
